<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\library\Sms as Smslib;
use app\common\model\user\Appoints;
use app\common\model\UserCards;
use think\Model;

/**
 * 示例接口
 */
class Demo extends Api
{

    //如果$noNeedLogin为空表示所有接口都需要登录才能请求
    //如果$noNeedRight为空表示所有接口都需要验证权限才能请求
    //如果接口已经设置无需登录,那也就无需鉴权了
    //
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['*'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['test2'];


    public function adddemo()
    {
        $irs =(new Appoints())->select();
        $saves[]=[];
        foreach ($irs as $k=>$v){
            $appoint_time=explode('-',$v['appoint_times']);
            $saves[]=[
                'id'=>$v['id'],
                'appoint_startime'=>strtotime($v['appoint_date'].$appoint_time[0]),
                'appoint_endtime'=>strtotime($v['appoint_date'].$appoint_time[1])];
        }
        (new Appoints())->isUpdate(true)->saveAll($saves);
    }


    public function orderitems()
    {
        $order = (new \app\common\model\order\Order())->where('order_sn','202309191311552903')->find();

        $order_items = db('order_item')->where(['order_id'=>$order['id']])->select();
        $time =time();

        foreach ($order_items as $k=>$v) {
            $msvae=[];
            db('order_item')->where(['id' => $v['id']])->update(['canuse_num' => $v['rights_num'] * $v['goods_num']]);
            for ($i = 1; $i <= $v['goods_num']; $i++) {
                $msvae[] = ['user_id' => $order['user_id'], 'order_item_id' => $v['id'],
                    'order_id' => $order['id'], 'hospital_ids' => $v['hospital_ids'],
                    'goods_id' => $v['goods_id'], 'card_id' => 0, 'type' => 1,
                    'rights_card' => $v['rights_card'], 'rights_title' => $v['rights_title'],
                    'rights_nums' => $v['rights_num'], 'can_uses' => $v['rights_num'],
                    'rights_memo' => $v['rights_memo'], 'rights_desc' => $v['rights_desc'],
                    'rights_endtime' => $v['rights_endtime'], 'rights_content' => $v['rights_content'],
                    'status' => 1
                ];
            }

            if (count($msvae) > 100) {
                $num = 100;//每次导入条数
                $limit = ceil(count($msvae) / $num);
                for ($i = 1; $i <= $limit; $i++) {
                    $offset = ($i - 1) * $num;
                    $data = array_slice($msvae, $offset, $num);
                    (new UserCards())->saveAll($data);
                };
            } else {
                (new UserCards())->saveAll($msvae);
            }
        }
    }

    public function tend()
    {
        $res = (new WxPay())->applyRefund1(6, 2,0.01*100);
        if(!$res['status']){
            $this->error($res['msg']);
        }
    }


    public function sendSms()
    {
        $config = get_addon_config('alisms');
        $template='SMS_463160557';
        $sign = '新眼光';
        $mobile='18862670515';
        $param=['name'=>'zw'];
        $alisms = new \addons\alisms\library\Alisms();
        $ret = $alisms->mobile($mobile)
            ->template($template)
            ->sign($sign)
            ->param($param)
            ->send();
        if ($ret) {
            $this->success("发送成功");
        } else {
            $this->error("发送失败！失败原因：" . $alisms->getError());
        }
    }
    public function sendSms1()
    {
        $config = get_addon_config('alisms');
        $template='SMS_463215561';
        $sign = $config['sign'];
        $mobile='18862670515';
        $param=['name'=>'zw','date'=>'2023-09-13','hosp'=>'镇江市第一人民医院'];
        $alisms = new \addons\alisms\library\Alisms();
        $ret = $alisms->mobile($mobile)
            ->template($template)
            ->sign($sign)
            ->param($param)
            ->send();
        if ($ret) {
            $this->success("发送成功");
        } else {
            $this->error("发送失败！失败原因：" . $alisms->getError());
        }
    }
    /**
     * 测试方法
     *
     * @ApiTitle    (测试名称)
     * @ApiSummary  (测试描述信息)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/demo/test/id/{id}/name/{name})
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="id", type="integer", required=true, description="会员ID")
     * @ApiParams   (name="name", type="string", required=true, description="用户名")
     * @ApiParams   (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回")
     * @ApiReturn   ({
         'code':'1',
         'msg':'返回成功'
        })
     */
    public function test()
    {
        $orders=  db('hospital_appointment')->where(['hospital_id'=>1])->order(['week_days'=>'desc'])->group('week_days')->column('week_name','week_days');
        $orders_w=  db('hospital_appointment')->where(['hospital_id'=>1])
            ->order(['week_days'=>'asc','startime'=>'asc'])
            ->field('id,startime,endtime,week_days')->select();
       // dump($orders_w);

        $times=[];
        foreach ($orders_w as $k => $v) {
            $times[$v['week_days']][] = $v;
        }
        $result=[];
        foreach ($orders as $k1=>$v1){
            $result[]=['name'=>$v1,'appiontment'=>$times[$k1]];
        }



        $this->success('返回成功', $result);

    }

    /**
     * 无需登录的接口
     *
     */
    public function test1()
    {
        $orders=  db('hospital_appointment')->where(['hospital_id'=>1])->order(['week_days'=>'desc'])->group('week_days')->column('week_name','week_days');
        $orders_w=  db('hospital_appointment')->where(['hospital_id'=>1])
            ->order(['week_days'=>'asc','startime'=>'asc'])
            ->field('id,startime,endtime,week_days')->select();
        // dump($orders_w);

        $times=[];
        foreach ($orders_w as $k => $v) {
            $startime=explode(':',$v['startime']);
            $endtime=explode(':',$v['endtime']);
            $times[$v['week_days']][] = ['start_hours'=>intval($startime[0]),
                'end_hours'=>intval($endtime[0]),
                'startime'=>$startime[0].':'.$startime[1],
                'endtime'=>$endtime[0].':'.$endtime[1],
                'time_solt'=>$startime[0].':'.$startime[1].'-'.$endtime[0].':'.$endtime[1]];
        }
        $result=[];
        foreach ($orders as $k1=>$v1){
            $result[]=['name'=>$v1,'appiontment'=>$times[$k1]];
        }
        $m=[];
        $m1=$result;
        $ms=array_column($result,'appiontment');
         $ms =array_unique($ms,SORT_REGULAR);


     //   dump($ms);
        $ms1=[];
        foreach ($ms as $_k=>$v_){
               $days=[];
               foreach ($m1 as $k2=>$v2){
//                   dump($v2);
//                   dump($v_);


                   if($v2['appiontment']==$v_){
                       $days[]=$v2['name'] ;
                   }
               }
               $count1=count($v_);
               $days=array_reverse($days);
               foreach ($v_ as $k3=>$v3){
                   if($k3<$count1-1){

                   }
               }

               $ms1[]=['days'=>$days,'appoint'=>$v_];
     ;
        }




        $this->success('返回成功', $ms1);
    }

    /**
     * 需要登录的接口
     *
     */
    public function test2()
    {
        $this->success('返回成功', ['action' => 'test2']);
    }

    /**
     * 需要登录且需要验证有相应组的权限
     *
     */
    public function test3()
    {
        $this->success('返回成功', ['action' => 'test3']);
    }

}
